<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2010 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml" version="1.0">
  <!-- Copyright 2010 Google Inc. All Rights Reserved -->
  <!-- Author jacoby@google.com (Jacoby Thwaites) -->

  <xsl:import href="common.xsl" />
  <xsl:import href="render.xsl" />

  <!--
    Key used to retrieve records by id, cross-referenced from the index
  -->
  <xsl:key name="contents" match="*[not(@name)][@id]" use="@id" />

  <!-- Composition tab behaviours -->
  <xsl:template match="composition" mode="head">
    <script type="text/javascript" src="../composition.js" />
    <xsl:apply-templates mode="head"/>
  </xsl:template>

  <!-- Horizontal nav when browsing called from common.xsl -->
  <xsl:template match="/composition" mode="horizontal-nav">
    <div class="nav">
      <div class="menu">

        <!--Browse shows the current root folder only with index -->
        <li>
          <a href="web_srv:contents?root={@root}&amp;xsl=/contents.xsl">
            Browse
          </a>
        </li>

        <!-- Composition is current tab -->
        <li>
          <a class="selected" href="#">
            Composition
          </a>
        </li>
        <li>
          <a href="#">
            Dry Run
          </a>
        </li>
      </div>
    </div>
  </xsl:template>

  <!-- A field index is rendered in left panel -->
  <xsl:template match="index[field]" mode="leftPanel">

    <!-- Render fields -->
    <div class="g-box g-box-sidelinks">
      <b>
        <xsl:text>Fields</xsl:text>
        <xsl:if test="@name and @name != ''">
          <xsl:text> in </xsl:text>
          <xsl:value-of select="@name"/>
        </xsl:if>
      </b>

      <xsl:apply-templates select="field" mode="leftPanel">
        <xsl:sort select="key('contents',@id)/@type" />
        <xsl:sort select="@name"/>
      </xsl:apply-templates>
    </div>

    <xsl:apply-templates select="index" mode="leftPanel"/>
  </xsl:template>

  <!-- Folder contents: fields render as linked name with parent -->
  <xsl:template match="index/field" mode="leftPanel">
    <xsl:variable name="genId" select="generate-id(key('fieldIndex',@id))"/>
    <div class="actionable" behaviour="highlightField" args="{$genId}">
      <div class="right">
        <xsl:value-of select="key('contents',@id)/@type"/>
      </div>
      <div class="left">
        <xsl:value-of select="@name" />
      </div>
      <div class="block"/>
    </div>
  </xsl:template>

  <!-- Help links in composition mode -->
  <xsl:template match="*" mode="help-links">
    <a href="#" class="g-sidenav-link">What is a composition?</a>
    <a href="#" class="g-sidenav-link">Why can I see nested boxes?</a>
  </xsl:template>

</xsl:stylesheet>
